
package com.dps.omc.action;

import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.dps.omc.service.IUploadFormatService;
import com.dps.omc.model.PreScript;
import com.dps.omc.service.IPreScriptService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dps.common.domain.view.BizData4Page;
import com.dps.common.utils.IdGenerator;
import com.dps.common.utils.JsonResult;
import com.dps.common.utils.PageParam;
import com.dps.common.utils.Constants;

@Controller
public class PreScriptAction extends AbstractAdminController<IPreScriptService>{

    @Autowired
    private IPreScriptService preScriptService;
    @Autowired
    private IUploadFormatService uploadFormatService;
    
    @RequestMapping(value="/prescript/list")
    public String list(PreScript preScript, ModelMap model, PageParam pageParam, HttpServletRequest request, HttpServletResponse response){

        //获取参数
    	Map<String, Object> conditions = getQueryMap(preScript); 	
    	BizData4Page<PreScript> pageCtx = doPage(model, conditions, pageParam);
    	model.addAttribute("preScript", preScript);
    	return "/module/prescript/list";
    }

    @RequestMapping(value="/prescript/lookup")
    public String lookup(PreScript preScript,ModelMap model,PageParam pageParam,HttpServletRequest request,HttpServletResponse response){

        //获取参数
        Map<String, Object> conditions = getQueryMap(preScript);
        BizData4Page<PreScript> pageCtx = doPage(model, conditions, pageParam);
        model.addAttribute("preScript", preScript);
        return "/module/prescript/lookup";
    }

    @RequestMapping(value="/prescript/add")
    public String add(ModelMap model,HttpServletRequest request,HttpServletResponse response){

    	model.addAttribute("preScript", new PreScript());
        model.addAttribute("uploadFormats",uploadFormatService.findAll());
        return "module/prescript/edit";
    }    
    
    @RequestMapping(value="/prescript/edit")
    public String edit(String id,ModelMap model,HttpServletRequest request,HttpServletResponse response){
    	
    	PreScript preScript = preScriptService.fetch(id);
    	model.addAttribute("preScript", preScript);
        model.addAttribute("uploadFormats",uploadFormatService.findAll());
        return "module/prescript/edit";
    }    
    
    @RequestMapping(value="/prescript/save")
    @ResponseBody
    public String save(HttpServletRequest request,PreScript preScript){
		boolean isAddNew = StringUtils.isBlank(preScript.getId())?true:false;
		if(isAddNew)
		{
			preScript.setId(IdGenerator.createNewId());
			preScriptService.add(preScript);
		}
		else
		{
			preScriptService.update(preScript);
		}
        return JsonResult.saveSuccessClose(Constants.MAIN_TAB).toString();
    }    
    
    @RequestMapping(value="/prescript/delete")
    @ResponseBody
    public String delete(String ids,HttpServletRequest request){
    	
    	preScriptService.deleteByIds(ids);
    	return JsonResult.DELETE_SUCCESS.toString();
    }       
	
    protected Map getQueryMap(PreScript preScript)
    {
    	Map<String, Object> conditions = new HashMap();
    	
		conditions.put("artifactId", preScript.getArtifactId());		
		conditions.put("artifactName", preScript.getArtifactName());		
		conditions.put("storePath", preScript.getStorePath());		
		conditions.put("storeName", preScript.getStoreName());		
		conditions.put("note", preScript.getNote());		
		conditions.put("createDate", preScript.getCreateDate());		
    	return conditions;
    }

    @Override
    protected IPreScriptService getMainService() {
        return preScriptService;
    }

    @Override
    protected String getMainObjName() {
        return "prescript";
    }

    @Override
    protected String getViewTitle() {
        return "prescript";
    }
}
